package cn.eeepay.framework.service.impl.aatAgentActivity;

import cn.eeepay.framework.dao.aatAgentActivity.AgentRewardRecordDao;
import cn.eeepay.framework.db.pagination.Page;
import cn.eeepay.framework.model.aatAgentActivity.AgentRewardRecord;
import cn.eeepay.framework.service.aatAgentActivity.AgentRewardRecordService;
import cn.eeepay.framework.util.ExportResult;
import cn.eeepay.framework.util.ListDataExcelExport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.*;

@Service("agentRewardRecordService")
public class AgentRewardRecordServiceImpl implements AgentRewardRecordService {

    private static final Logger log = LoggerFactory.getLogger(AgentRewardRecordServiceImpl.class);

    @Resource
    private AgentRewardRecordDao agentRewardRecordDao;


    @Override
    public List<AgentRewardRecord> selectAllList(AgentRewardRecord info, Page<AgentRewardRecord> page) {
        return  agentRewardRecordDao.selectAllList(info,page);
    }

    @Override
    public void exportExcel(AgentRewardRecord info, HttpServletResponse response) throws Exception{
        List<AgentRewardRecord> list = agentRewardRecordDao.exportSelect(info);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        OutputStream ouputStream = null;
        String fileName = "领奖记录" + sdf.format(new Date()) + ".xls";
        String fileNameFormat = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
        response.setHeader("Content-disposition", "attachment;filename=" + fileNameFormat);
        List<Map<String, String>> data = new ArrayList<Map<String, String>>();

        Map<String,String> actTypeMap=new HashMap<String,String>();
        actTypeMap.put("1","百团大战");
        actTypeMap.put("2","千团大战");

        if(list.size()<1){
            Map<String, String> maps = new HashMap<String, String>();
            maps.put("actCode", null);
            maps.put("actName", null);
            maps.put("actType", null);
            maps.put("rankSeq", null);
            maps.put("rewardName", null);
            maps.put("agentNo",null);
            maps.put("agentName",null);
            maps.put("mobilephone", null);
            maps.put("oneAgentNo", null);
            maps.put("oneAgentName",null);
            maps.put("oneSaleName",null);
            maps.put("createTime",null);
            data.add(maps);
        }else{
            for (AgentRewardRecord item : list) {
                Map<String, String> maps = new HashMap<>();
                maps.put("actCode", item.getActCode());
                maps.put("actName", item.getActName());
                maps.put("actType", actTypeMap.get(item.getActType()));
                maps.put("rankSeq", item.getRankSeq()==null?null:item.getRankSeq().toString());
                maps.put("rewardName", item.getRewardName());
                maps.put("agentNo", item.getAgentNo());
                maps.put("agentName", item.getAgentName());
                maps.put("mobilephone", item.getMobilephone());
                maps.put("oneAgentNo", item.getOneAgentNo());
                maps.put("oneAgentName", item.getOneAgentName());
                maps.put("oneSaleName", item.getOneSaleName());
                maps.put("createTime",item.getCreateTime()==null?"":sdf1.format(item.getCreateTime()));
                data.add(maps);
            }
        }
        ListDataExcelExport export = new ListDataExcelExport();
        String[] cols = new String[]{"actCode","actName", "actType", "rankSeq", "rewardName","agentNo", "agentName", "mobilephone",
                "oneAgentNo", "oneAgentName", "oneSaleName", "createTime"};
        String[] colsName = new String[]{"活动编号","活动名称","子活动名称","排名","奖项","代理商编号","代理商名称","手机号码",
                "一级代理商编号","一级代理商名称","一级代理商所属销售","领取时间"};
        try {
            ouputStream = response.getOutputStream();
            export.export(cols, colsName, data, response.getOutputStream());
        } catch (Exception e) {
            ExportResult.setExportResult(response,false,"导出异常");
            log.error("导出异常", e);
        } finally {
            try {
                if (ouputStream != null) {
                    ouputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
